//
//  NewMainBottomView.m
//  QoilPublic
//
//  Created by SunChao on 2018/1/12.
//  Copyright © 2018年 孙超. All rights reserved.
//

#import "NewMainBottomView.h"
#import "QMainModel.h"
#import "CustomView.h"
#import "QPointShopVC.h"
#import "QPointsVC.h"
#import "QActivityWebVC.h"
#import "QTawardVC.h"
#import "DaiBanWebVC.h"
#import "QLoginVC.h"
#import "SKAutoScrollLabel.h"
#import "QActiveWebViewVC.h"

#define margin 15
#define padding  5
@interface NewMainBottomView()

@property (weak, nonatomic) IBOutlet UIView *notiBgView;
@property (weak, nonatomic) IBOutlet UIView *notiView;
@property (weak, nonatomic) IBOutlet UIView *scroBackgroundView;
@property (strong,nonatomic) NSMutableArray *dataSource;
@property (strong,nonatomic) UIScrollView *scrollView;
@property (strong,nonatomic) SKAutoScrollLabel *scrollLabel;
@property (strong,nonatomic) NSString *tapUrl;
@end

@implementation NewMainBottomView

-(void)awakeFromNib{
    
    [super awakeFromNib];
    UIView *view = [UIView new];
    view.frame = _notiBgView.bounds;
    view.backgroundColor = [UIColor clearColor];
    [_notiBgView addSubview:view];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [view addGestureRecognizer:tap];

}

- (void)tap{
    
    UIViewController *vc = [QViewUtility getCurrentVC];
    QActivityWebVC *webVC = [[QActivityWebVC alloc]init];
    [vc.navigationController pushViewController:webVC animated:YES];
    webVC.url = _tapUrl;
}

- (void)setNewsWithData:(NSMutableArray *)array{
    
    NSDictionary *dic = [array objectAtIndex:0];
    NSString *newString = [dic objectForKey:@"imgUrl"];
    _tapUrl = [dic objectForKey:@"url"];
    if (_scrollLabel) {
        [_scrollLabel removeFromSuperview];
    }
    _scrollLabel = [[SKAutoScrollLabel alloc] initWithFrame:CGRectMake(0, 0, _notiView.frame.size.width, _notiView.frame.size.height)];
    [_notiView addSubview:_scrollLabel];
    _scrollLabel.backgroundColor = [UIColor whiteColor];
    _scrollLabel.textColor = [UIColor blackColor];
    _scrollLabel.font = [UIFont systemFontOfSize:16];
    _scrollLabel.direction = SK_AUTOSCROLL_DIRECTION_LEFT;
    _scrollLabel.text = newString;
}
-(void)setBannerWithData:(NSMutableArray *)array{
    
    self.dataSource = array.mutableCopy;
    CGFloat imgW = (kScreenWidth - margin - 3*padding)/3.5 ;
    CGFloat imgH = _scroBackgroundView.frame.size.height;
    if (_scrollView) {
        [_scrollView removeFromSuperview];
    }
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 5, _scroBackgroundView.frame.size.width, _scroBackgroundView.frame.size.height)];
    _scrollView.contentSize = CGSizeMake(array.count*(imgW+padding)+margin, imgH);
    [_scroBackgroundView addSubview:_scrollView];
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    for (int i = 0; i<array.count; i++) {
        
        CustomView  *view = [[CustomView alloc]initWithFrame:CGRectMake(i*(imgW+padding)+15, 0, imgW, 130)];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.cornerRadius = 10.0f;
        view.layer.shadowColor = [UIColor blackColor].CGColor;
        view.layer.shadowOpacity = 0.25f;
        view.layer.shadowRadius = 4.f;
        view.layer.shadowOffset = CGSizeMake(4,4);
        [_scrollView addSubview:view];
        
        QMainModel *model = [array objectAtIndex:i];
        NSString *imgUrl = model.imgUrl;
        NSString *title = model.title;
        NSString *sort = model.sort;
        NSInteger sortNum = [sort integerValue];
        [view.cButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        view.cButton.tag = sortNum+100;
        [view.cImgView sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:[UIImage imageNamed:@"Main_shop.png"]];
        view.cLabel.text = title;
        
    }
    
}
#pragma --mark banner点击跳转
- (void)buttonAction:(UIButton *)sender{
    
    NSLog(@"sender:%ld",(long)sender.tag);
    int index = (int)sender.tag - 100;
    
    switch (index) {
            
        case 1://积分商城
        {
            if([QUserInfo sharedInfo].loginModel){
                QPointShopVC * vc = [[QPointShopVC alloc]init];
                UIViewController *currentVC = [QViewUtility getCurrentVC];
                [currentVC.navigationController pushViewController:vc animated:YES];
            }else{
                [self notLogin];
            }
        }
            break;
        case 2://积分签到
        {
            if([QUserInfo sharedInfo].loginModel){
                QPointsVC * vc = [[QPointsVC alloc] init];
                UIViewController *currentVC = [QViewUtility getCurrentVC];
                [currentVC.navigationController pushViewController:vc animated:YES];
            }else{
                [self notLogin];
            }
            
        }
            break;
        case 3://设备管理
        {
            if([QUserInfo sharedInfo].loginModel){
                QActivityWebVC *vc = [[QActivityWebVC alloc] init];
                UIViewController *currentVC = [QViewUtility getCurrentVC];
                [currentVC.navigationController pushViewController:vc animated:YES];
                vc.url = [NSString stringWithFormat:@"http://www.ydaol.com/oilWX_api/client/EquipmentList.html?token=%@",[QUserInfo sharedInfo].loginModel.token];
            }else{
                [self notLogin];
            }
            
        }
            break;
            
        case 4://推荐有奖4
        {
            
            if([QUserInfo sharedInfo].loginModel){
                QTawardVC *vc = [[QTawardVC alloc] init];
                UIViewController *currentVC = [QViewUtility getCurrentVC];
                
                [currentVC.navigationController pushViewController:vc animated:YES];
                vc.textString = @"1";
            }else{
                [self notLogin];
            }
        }
            break;
        case 5://邀请好友5
        {
            if([QUserInfo sharedInfo].loginModel){
                QTawardVC *vc = [[QTawardVC alloc] init];
                UIViewController *currentVC = [QViewUtility getCurrentVC];
                [currentVC.navigationController pushViewController:vc animated:YES];
                vc.textString = @"2";
                
            }else{
                
                [self notLogin];
            }
        }
            break;
        case 6://机手带班联盟
        {
            if([QUserInfo sharedInfo].loginModel){
                DaiBanWebVC *vc = [[DaiBanWebVC alloc] init];
                UIViewController *currentVC = [QViewUtility getCurrentVC];
                [currentVC.navigationController pushViewController:vc animated:YES];
                QMainModel *model = [self.dataSource objectAtIndex:4];
                vc.url = model.htmlUrl;
                
            }else{
                
                [self notLogin];
            }
        }
            break;
            
        default:
        {
             if([QUserInfo sharedInfo].loginModel){
            QActivityWebVC *vc = [[QActivityWebVC alloc] init];
            UIViewController *currentVC = [QViewUtility getCurrentVC];
            [currentVC.navigationController pushViewController:vc animated:YES];
            QMainModel *model = [self.dataSource objectAtIndex:index-2];
            vc.title = model.title;
            vc.url = model.htmlUrl;
            
             }else{
                 
                 [self notLogin];
             }
        }
            break;
    }
    
}
#pragma mark --用户是否登陆
- (void)notLogin{
    
    [QViewUtility showAlertChooseViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"您还未登录,请登录" leftTitle:@"取消" rightTitle:@"立即登录" type:1 myBlock:^(NSInteger tag) {
        if(tag == 1){
            QLoginVC *vc = [[QLoginVC alloc] init];
            UIViewController *currentVC = [QViewUtility getCurrentVC];
            [currentVC.navigationController pushViewController:vc animated:YES];
        }
    }];
}


ARRAY_INIT(dataSource);
@end
